package com.uchengwang.ui.cm_ui.ucity.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.adapter.BaseCycleAdapter;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.BaseViewHolder;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.utils.WidgetUtil;

import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2018/1/30.
 * 粉丝 关注 好友等
 */

public class UserAdapter extends BaseCycleAdapter<UserBean, RecyclerView.ViewHolder>{
    private Context context;
    private int type;//1粉丝 2关注 3好友
    public UserAdapter(Context context, int type){
        this.context = context;
        this.type = type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_view, parent, false);
        return new BaseViewHolder(view, type);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        final BaseViewHolder holder = (BaseViewHolder) h;
        final UserBean bean = list.get(position);
        String userAvatar = bean.getUserAvatar();
        if (TextUtils.isEmpty(userAvatar)){
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.default_image,  ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, userAvatar,  ImageManager.ImageType.CIRCLE);
        }
        final String showName = (!TextUtils.isEmpty(bean.getRemarkName()) ? bean.getRemarkName() : bean.getNickName()) ;
        holder.name.setText(showName);

        if (bean.getUserRole() == BaseConfig.STUDENT_ROLE){
            ImageManager.getInstance().loadImage(context, holder.apply_sex, bean.getSex() ==0 ?R.drawable.icon_woman : R.drawable.icon_man, ImageManager.ImageType.NARMOL);
        }

        String uniName = bean.getUniName();
        if (!TextUtils.isEmpty(uniName)){
            holder.des.setText(uniName);
        }else {
            holder.des.setText("暂无学校信息");
        }
        switch (type){//1粉丝 2关注 3好友
            case 1:
                WidgetUtil.getUtil().setAttenWidget(holder.bt, bean.isFollow());
                holder.bt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        follow(bean);
                    }
                });
                break;
            case 2:

                break;
            case 3:
                WidgetUtil.getUtil().setChatWidget(holder.bt);
                holder.bt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UCommonUtil.getUtil().toChatActiviy(context, bean.getId(), Conversation.ConversationType.PRIVATE, showName);
                    }
                });
                break;
        }
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 关注/取消关注
     * */
    private void follow(final UserBean bean){
        HttpMeManager.getPresenter().addAtten((Activity) context, !bean.isFollow(), bean.getId(), new HttpMeManager.AttenListener() {
            @Override
            public void atten(int status) {
                if (status != 0){
                    UCommonUtil.getUtil().showToast(context, "操作失败");
                    return;
                }
                if (status == 0){
                    UCommonUtil.getUtil().showToast(context, "关注成功");
                    list.remove(bean);
                    notifyDataSetChanged();
                }
            }
        });
    }
}
